package com.linked.mystyle.leaveword.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linked.mystyle.leaveword.bean.LeaveWordListRequest;
import com.linked.mystyle.leaveword.bean.LeaveWordPO;
import com.linked.mystyle.leaveword.bean.LeaveWordReturnPO;
import com.linked.mystyle.leaveword.mapper.ILeaveWordMapper;
import com.linked.mystyle.leaveword.mapper.ILeaveWordReturnMapper;
import com.linked.mystyle.leaveword.service.ILeaveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author :dbq
 * @date : 2022/9/30 14:23
 */
@Service
public class LeaveWordServiceImpl implements ILeaveWordService {

    private final ILeaveWordMapper leaveWordMapper;

    private final ILeaveWordReturnMapper leaveWordReturnMapper;

    @Autowired
    public LeaveWordServiceImpl(ILeaveWordMapper leaveWordMapper, ILeaveWordReturnMapper leaveWordReturnMapper) {
        this.leaveWordMapper = leaveWordMapper;
        this.leaveWordReturnMapper = leaveWordReturnMapper;
    }

    @Override
    public Boolean addALeaveWord(LeaveWordPO request) throws Exception {
        return leaveWordMapper.insert(request) > 0;
    }

    @Override
    public IPage<LeaveWordPO> queryLeaveWordList(LeaveWordListRequest request) throws Exception {

        return leaveWordMapper.queryLeaveWordList(request);
    }

    @Transactional
    @Override
    public Boolean returnLeaveWord(LeaveWordReturnPO request) throws Exception {

        int ret = leaveWordReturnMapper.insert(request);
        int finalRes = 0;
        if (ret > 0) {
            finalRes = leaveWordMapper.updateHandleStatus(request.getLeavewordId(), "1");
        }
        return finalRes > 0;
    }

    @Override
    @Transactional()
    public boolean deleteLeaveWord(LeaveWordPO param) throws Exception {
        int ret = leaveWordMapper.deleteById(param.getLeavewordId());
        QueryWrapper<LeaveWordReturnPO> qw = new QueryWrapper<>();
        qw.eq("leaveword_id", param.getLeavewordId());
        leaveWordReturnMapper.delete(qw);
        return ret > 0;
    }
}
